﻿package  view.player{
	import flash.display.MovieClip
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	[Embed(source="../../../lib/TimeSaver.swf", symbol="classes.Player")]
	public class Player extends CommonBehave{
		public static var STATE_JUMP:int = 11;
		public static var STATE_DROP:int = 12;
		
		public var con:int = 0; //组合键不会漂移，单键时基数可以走，偶数不能走
		public var bAttack:Boolean = false;//是否攻击
		public var bLife:Boolean = false;//是否被攻击
		public var flag:int = 0;//判断主角面朝哪个方向0：UP 1：LEFT 2:RIGHT 3:DOWN
        public var JumpTime:int = 5;//jump次数
		
		public function Player() {
			// constructor code
			m_Speed = 15;
			m_Attack = 3;
			
			this.attackBox = MovieClip(this.getChildByName("attackBox"));
			this.bodyBox = MovieClip(this.getChildByName("bodyBox"));
			//createTextLeft();
			//createTextRight();
			Reset();
		}
		public function Reset(posX:int = 50,posY:int = 600):void
		{
			this.x = posX;
			this.y = posY;
			m_LastX = this.x;
			m_LastY = this.y;
		}
		
		//处理逻辑，控制移动
		public function Logic():void
		{
			switch(m_nState)
			{
				case STATE_JUMP:
				      bflag = false;//保护JUMP时Y位置不会被限制，不会被键盘输入打段
				      updateJump();//跳起
					  break;
			    case STATE_DROP:
					  updateDrop();//降落
					  break;
				case STATE_ATTACK:
				      bAttack = true;
					  if(flag == 2)//右边
					     FrameUpDate(21,29);
					  else if(flag == 1)//左边
					     FrameUpDate(30,38);
					  setState(STATE_STOP);
				      break;
				case STATE_BEHIT:
				      bLife = true;trace("behit");
					  setState(STATE_STOP);
				      break;
				case STATE_DEAD:
				      //Reset();
				      break;
				default:
				      break;
				}
			
			}
		
		public function Input(keys:Array):void
		{
			if(m_nState == STATE_BEHIT)
				return;					//被攻击时，所有的输入都无效*/
			var bInput:Boolean = false;
			var bMove:Boolean = false;
			for each(var key:int in keys)
			{
			  switch(key)
			  {
				  case Keyboard.W:
					   flag = 0;
					   con++;
					   m_nMove = STATE_UP;
					   Move();
					   if(con % 2 != 0)
					       FrameUpDate(1,5);
					   bInput = true;
					   bMove = true;
					   break;
				  case Keyboard.D:
					   flag = 2;
					   con++;
					   m_nMove = STATE_RIGHT;
					   Move();
					   if(con % 2 != 0)
					      FrameUpDate(11,15);
					   bInput = true;
					   bMove = true;
					   break;
				  case Keyboard.S:
					   flag = 3;
					   con++;
					   m_nMove = STATE_DOWN;
					   Move();
					   if(con % 2 != 0)
					      FrameUpDate(16,20);
					   bInput = true;
					   bMove = true;
					   break;
				  case Keyboard.A:
					   flag = 1;
					   con++;
					   m_nMove = STATE_LEFT;
					   Move();
					   if(con % 2 != 0)
					      FrameUpDate(6,10);
					   bInput = true;
					   bMove = true;
					   break;
				  case Keyboard.L:
					   setState(STATE_JUMP);
					   bInput = true;
					   bMove = true;
					   break;
				  case Keyboard.J:
				      // if(!bMove)
					      setState(STATE_ATTACK);
					   bInput = true;
					   break;
				  default:
				       break;
				}				  
			}
			if (bInput == false)
				setState(STATE_STOP);
		}
		
		public function updateJump():void
		{
			switch(flag)
			{
				case 0:
				     FrameUpDate(1,1);
					 m_nMove = STATE_UP;
				     Move();
					 break;
			    case 1:
				     FrameUpDate(6,6);
					 m_nMove = STATE_UP;
				     Move();
					 break;
			    case 2:
				     FrameUpDate(11,11);
					 m_nMove = STATE_UP;
				     Move();
					 break;
				case 3:
				     FrameUpDate(16,16);
					 m_nMove = STATE_UP;
				     Move();
					 break;
				}
			
			JumpTime -- ;
		 	if(JumpTime < 0)
			{
			    setState(STATE_DROP);
				JumpTime = 5;
			}
			
			else
			    setState(STATE_JUMP);
		}
		
		public function updateDrop():void
		{
			switch(flag)
			{
				case 0:
				     FrameUpDate(1,1);
					 m_nMove = STATE_DOWN;
				     Move();
					 break;
			    case 1:
				     FrameUpDate(6,6);
					 m_nMove = STATE_DOWN;
				     Move();
					 break;
			    case 2:
				     FrameUpDate(11,11);
					 m_nMove = STATE_DOWN;
				     Move();
					 break;
				case 3:
				     FrameUpDate(16,16);
					 m_nMove = STATE_DOWN;
				     Move();
					 break;
			}
				
			JumpTime -- ;
			if(JumpTime < 0)
			{
				setState(STATE_STOP);
				JumpTime = 5;
				bflag = true;
			}
			else
			    setState(STATE_DROP);
		}
			
	}
}
